/**
 * 
 */
package com.nari.heatassist.service;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;

import com.nari.baseinfo.dao.IHeatRoomDao;
import com.nari.baseinfo.model.IHeatRoom;
import com.nari.heatassist.utils.ConsumeStatisReportUtil;
import com.nari.heatassist.utils.ReportBuildupData;
import com.nari.heatshare.dao.IUserShareResultDao;
import com.nari.heatshare.model.IUserShareResult;
import com.nari.platform.date.CalendarConstants;
import com.nari.platform.date.CalendarUtils;
import com.nari.platform.model.ResponseMsg;
import com.nari.platform.utils.NumberSystemConversion;

/**
 * 
 * @author Xiaopeng
 * @date 2014-4-30
 */
public class RoomConsumeStatisServiceImpl implements IRoomConsumeStatisService {
	private IHeatRoomDao iHeatRoomDao;
	private IUserShareResultDao iUserShareResultDao;
	
	/**
	 * 查询热用户对应时段的耗热报表数据
	 * @param heatRoomList
	 * @param startDate
	 * @param endDate
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-4-30
	 */
	public ReportBuildupData queryBuildingConsumeReport(List<IHeatRoom> heatRoomList, Date startDate, 
			Date endDate) throws Exception {
		//报表数据
		StringBuffer reportData = new StringBuffer("");
		//未在指定时段参加账单分摊的用户信息
		StringBuffer problemRoomConfig = new StringBuffer("");
		if(heatRoomList!=null && heatRoomList.size()>0) {
			for(int index=0; index<heatRoomList.size(); index++) {
				IHeatRoom heatRoom = heatRoomList.get(index);
				//统计指定时段的用户账单数量
				int userResultCounter = iUserShareResultDao.countUserResultByPeriod(heatRoom.getId(), startDate, endDate);
				if(userResultCounter > 0) {
					String roomPerConsumeFormat = this.calcRoomPerConsumeFormat(heatRoom, startDate, endDate);
					//用户耗热报表
					String roomReportData = ConsumeStatisReportUtil.gen2DReportData(heatRoom.getHouseNo(), roomPerConsumeFormat);
					reportData.append(roomReportData);
				} else {
					problemRoomConfig.append(heatRoom.getHouseNo());
					problemRoomConfig.append("、");
				}
			}
		}
		//报告未分摊用户
		ResponseMsg rmsg = ResponseMsg.getInstance(true, "");
		if(problemRoomConfig.length() > 0) {
			String msg = "请检查以下用户在时间周期[" 
						+ CalendarUtils.parseDateToString(startDate)
						+ "至"
						+ CalendarUtils.parseDateToString(endDate)
						+"]内是否参加过热费分摊："
						+ problemRoomConfig.substring(0, problemRoomConfig.length()-1);
			rmsg = ResponseMsg.getInstance(false, msg);
		}
		return ReportBuildupData.getInstance(rmsg, reportData.toString());
	}
	
	/**
	 * 查询单一用户不同统计时段的耗热分析报表
	 * @param heatRoom
	 * @param startDate
	 * @param endDate
	 * @param timeFrequence 统计频率
	 * permonth：每月	peryear：每年
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-5-3
	 */
	public ReportBuildupData queryRoomConsumeByFrequency(IHeatRoom heatRoom, Date startDate, 
			Date endDate, String timeFrequence) throws Exception {
		//报表数据
		StringBuffer reportData = new StringBuffer("");
		//未在指定时段参加账单分摊的用户信息
		StringBuffer problemRoomConfig = new StringBuffer("");
		//年度统计
		if("peryear".equals(timeFrequence)) {
			//时段在同一年
			if(CalendarUtils.getYearFromCal(startDate) == CalendarUtils.getYearFromCal(endDate)) {
				//统计指定时段的用户账单数量
				int userResultCounter = iUserShareResultDao.countUserResultByPeriod(heatRoom.getId(), startDate, endDate);
				if(userResultCounter > 0) {
					String roomPerConsumeFormat = this.calcRoomPerConsumeFormat(heatRoom, startDate, endDate);
					//用户耗热报表
					String reportLabel = CalendarUtils.parseDateToString(startDate, "yyyy");
					String roomReportData = ConsumeStatisReportUtil.gen2DReportData(reportLabel, roomPerConsumeFormat);
					reportData.append(roomReportData);
				} else {
					problemRoomConfig.append(heatRoom.getHouseNo());
					problemRoomConfig.append("(");
					problemRoomConfig.append(CalendarUtils.parseDateToString(startDate));
					problemRoomConfig.append("至");
					problemRoomConfig.append(CalendarUtils.parseDateToString(endDate));
					problemRoomConfig.append(")");
					problemRoomConfig.append("、");
				}
			} else { //时段不在同一年
				int startYear = CalendarUtils.getYearFromCal(startDate);
				int endYear = CalendarUtils.getYearFromCal(endDate);
				while(CalendarUtils.getYearFromCal(startDate) <= endYear) {
					Date reportStartDate = null;
					Date reportEndDate = null;
					//获取报表起止日期
					int currYear = CalendarUtils.getYearFromCal(startDate);
					if(currYear == startYear) {
						reportStartDate = startDate;
						reportEndDate = CalendarUtils.parseStringToDate(currYear+"-12-31");
					} else if(currYear>startYear && currYear<endYear) {
						reportStartDate = CalendarUtils.parseStringToDate(currYear+"-01-01");
						reportEndDate = CalendarUtils.parseStringToDate(currYear+"-12-31");
					} else if(currYear == endYear) {
						reportStartDate = CalendarUtils.parseStringToDate(currYear+"-01-01");
						reportEndDate = endDate;
					}
					//统计指定时段的用户账单数量
					int userResultCounter = iUserShareResultDao.countUserResultByPeriod(heatRoom.getId(), reportStartDate, reportEndDate);
					if(userResultCounter > 0) {
						String roomPerConsumeFormat = this.calcRoomPerConsumeFormat(heatRoom, reportStartDate, reportEndDate);
						//用户耗热报表
						String reportLabel = CalendarUtils.parseDateToString(reportStartDate, "yyyy");
						String roomReportData = ConsumeStatisReportUtil.gen2DReportData(reportLabel, roomPerConsumeFormat);
						reportData.append(roomReportData);
					} else {
						problemRoomConfig.append(heatRoom.getHouseNo());
						problemRoomConfig.append("(");
						problemRoomConfig.append(CalendarUtils.parseDateToString(reportStartDate));
						problemRoomConfig.append("至");
						problemRoomConfig.append(CalendarUtils.parseDateToString(reportEndDate));
						problemRoomConfig.append(")");
						problemRoomConfig.append("、");
					}
					//年度累加
					startDate = CalendarUtils.addYears(startDate, 1);
				}
			}
		}
		//月度统计
		if("permonth".equals(timeFrequence)) {
			//时段在同一月
			if(CalendarUtils.parseDateToString(startDate, "yyyyMM").equals(CalendarUtils.parseDateToString(endDate, "yyyyMM"))) {
				//统计指定时段的用户账单数量
				int userResultCounter = iUserShareResultDao.countUserResultByPeriod(heatRoom.getId(), startDate, endDate);
				if(userResultCounter > 0) {
					String roomPerConsumeFormat = this.calcRoomPerConsumeFormat(heatRoom, startDate, endDate);
					//用户耗热报表
					String reportLabel = CalendarUtils.parseDateToString(startDate, "yyyy.MM");
					String roomReportData = ConsumeStatisReportUtil.gen2DReportData(reportLabel, roomPerConsumeFormat);
					reportData.append(roomReportData);
				} else {
					problemRoomConfig.append(heatRoom.getHouseNo());
					problemRoomConfig.append("(");
					problemRoomConfig.append(CalendarUtils.parseDateToString(startDate));
					problemRoomConfig.append("至");
					problemRoomConfig.append(CalendarUtils.parseDateToString(endDate));
					problemRoomConfig.append(")");
					problemRoomConfig.append("、");
				}
			} else { //时段不在同一月
				int startMonth = Integer.valueOf(CalendarUtils.parseDateToString(startDate, "yyyyMM"));
				int endMonth = Integer.valueOf(CalendarUtils.parseDateToString(endDate, "yyyyMM"));
				while(Integer.valueOf(CalendarUtils.parseDateToString(startDate, "yyyyMM")) <= endMonth) {
					Date reportStartDate = null;
					Date reportEndDate = null;
					//获取报表起止日期
					int currMonth = Integer.valueOf(CalendarUtils.parseDateToString(startDate, "yyyyMM"));
					if(currMonth == startMonth) {
						reportStartDate = startDate;
						//当月总天数
						int dayNumbers = CalendarUtils.getNumberOfDaysInMonth(CalendarUtils.getYearFromCal(startDate), CalendarUtils.getMonthFromCal(startDate));
						reportEndDate = CalendarUtils.parseStringToDate(currMonth+""+dayNumbers, CalendarConstants.dateFormat_stamp);
					} else if(currMonth>startMonth && currMonth<endMonth) {
						reportStartDate = CalendarUtils.parseStringToDate(currMonth+"01", CalendarConstants.dateFormat_stamp);
						//当月总天数
						int dayNumbers = CalendarUtils.getNumberOfDaysInMonth(CalendarUtils.getYearFromCal(startDate), CalendarUtils.getMonthFromCal(startDate));
						reportEndDate = CalendarUtils.parseStringToDate(currMonth+""+dayNumbers, CalendarConstants.dateFormat_stamp);
					} else if(currMonth == endMonth) {
						reportStartDate = CalendarUtils.parseStringToDate(currMonth+"01", CalendarConstants.dateFormat_stamp);
						reportEndDate = endDate;
					}
					//统计指定时段的用户账单数量
					int userResultCounter = iUserShareResultDao.countUserResultByPeriod(heatRoom.getId(), reportStartDate, reportEndDate);
					if(userResultCounter > 0) {
						String roomPerConsumeFormat = this.calcRoomPerConsumeFormat(heatRoom, reportStartDate, reportEndDate);
						//用户耗热报表
						String reportLabel = CalendarUtils.parseDateToString(reportStartDate, "yyyy.MM");
						String roomReportData = ConsumeStatisReportUtil.gen2DReportData(reportLabel, roomPerConsumeFormat);
						reportData.append(roomReportData);
					} else {
						problemRoomConfig.append(heatRoom.getHouseNo());
						problemRoomConfig.append("(");
						problemRoomConfig.append(CalendarUtils.parseDateToString(reportStartDate));
						problemRoomConfig.append("至");
						problemRoomConfig.append(CalendarUtils.parseDateToString(reportEndDate));
						problemRoomConfig.append(")");
						problemRoomConfig.append("、");
					}
					//月度累加
					startDate = CalendarUtils.addMonths(startDate, 1);
				}
			}
		}
		//报告未分摊用户
		ResponseMsg rmsg = ResponseMsg.getInstance(true, "");
		if(problemRoomConfig.length() > 0) {
			String msg = "请检查以下用户在指定时间周期内是否参加过热费分摊："
						+ problemRoomConfig.substring(0, problemRoomConfig.length()-1);
			rmsg = ResponseMsg.getInstance(false, msg);
		}
		return ReportBuildupData.getInstance(rmsg, reportData.toString());
	}
	
	/**
	 * 计算热用户指定时段的单位面积耗热量(KWh/㎡)
	 * @param heatRoom
	 * @param startDate
	 * @param endDate
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-5-3
	 */
	private String calcRoomPerConsumeFormat(IHeatRoom heatRoom, Date startDate, 
			Date endDate) throws Exception {
		//查询指定时段内用户分摊账单
		List<IUserShareResult> userResultList = iUserShareResultDao.queryUserResultByPeriod(heatRoom.getId(), startDate, endDate);
		//取默认最大参考耗热量
		IUserShareResult userResult = userResultList.get(0);
		//单位耗热量
		double perAreaConsume = 0;
		if(NumberSystemConversion.compareFloatValue(heatRoom.getStructArea(), 0) > 0) {
			perAreaConsume = new BigDecimal(userResult.getReferHeatConsume())
					.divide(new BigDecimal(heatRoom.getStructArea()), new MathContext(6, RoundingMode.HALF_UP))
					.doubleValue();
		}
		return NumberSystemConversion.formatToDecimalNumber(perAreaConsume);
	}
	
	/**
	 * @return the iHeatRoomDao
	 */
	public IHeatRoomDao getiHeatRoomDao() {
		return iHeatRoomDao;
	}

	/**
	 * @param iHeatRoomDao the iHeatRoomDao to set
	 */
	public void setiHeatRoomDao(IHeatRoomDao iHeatRoomDao) {
		this.iHeatRoomDao = iHeatRoomDao;
	}

	/**
	 * @return the iUserShareResultDao
	 */
	public IUserShareResultDao getiUserShareResultDao() {
		return iUserShareResultDao;
	}

	/**
	 * @param iUserShareResultDao the iUserShareResultDao to set
	 */
	public void setiUserShareResultDao(IUserShareResultDao iUserShareResultDao) {
		this.iUserShareResultDao = iUserShareResultDao;
	}
	
}
